<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Controller for managing form generator.
 * @package cms-generator/conrollers
 * @category Controller
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2014
 */
class Generate extends CMS_Controller {

    /**
     *  Constructor method.
     */
    function __construct()
    {
        parent::__construct();
        
        $this->load->model('generator_model');
        
        $this->load->helper('generator');
        $this->load->helper('form_generator');
        
        $this->load->library('form_validation');
    }

    /**
     * Default method called when nothing is specified.
     * Displays form generator.
     * @access public
     */
    public function index()
    {
        // active navigation
        $data['navigation'] = 'generator';

        // build breadcrumb
        $this->breadcrumb->clear();
        $this->breadcrumb->add_crumb('Dashboard');

        // build template
        $this->template
                ->title('Dashboard')
                ->set_layout('main')
                ->set_partial('navigation', 'layouts/navigation')
//                ->set_partial('footer', 'layouts/admin/footer')
                ->build('generator', $data);
        
    }
    
    /**
     * Generate custom captcha
     * @access public
     */
    public function get_captcha()
    {
        if( valid_captcha() )
        {
            $length         = set_value('length')       ? set_value('length') : '6';
//            $path           = set_value('path')         ? './' . set_value('path') : './generated_contents/captcha';
            $width          = set_value('width')        ? set_value('width') : '300';
            $height         = set_value('height')       ? set_value('height') : '100';
            $expiration     = set_value('expiration')   ? set_value('expiration') : '900';
            
            $captcha = generate_captcha($length, $expiration, $width, $height);
            echo $captcha['image'];
        
        } else
        {
            echo 'Invalid Parameters';
        }
    }
    
    /**
     * Retrieve data for radio buttons, checkboxes and dropdown menus
     * @access public
     */
    public function retrieve_data()
    {
        if( valid_retrieve_data() )
        {
            $db_table   = set_value('db-name');
            $value      = set_value('db-attr-value');
            $text       = set_value('db-attr-text');
            $image      = set_value('db-attr-image');
            
            $data = $this->generator_model->retrieve_data($db_table, $value, $text, $image);
            
            if( $data OR count($data) === 0)
            {
                echo json_encode($data);
            } else
            {
                $data = array();
                for($i =1; $i < 4; $i++)
                {
                    if($image == 'none')
                    {
                        array_push($data, array('value' => "$value $i", 'text' => "$text $i"));
                    } else
                    {
                        array_push($data, array('value' => "$value $i", 'text' => "$text $i", 'image' => "$image $i"));
                    }
                }
                echo json_encode($data);
            }
        } else
        {
            echo json_encode( array('error' => 'error') );
        }
    }
}

/* End of file generator.php */
/* Location: ./application/modules/cms-generator/controllers/generator.php */